package com.eight.cloud.springmvc.utils.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
 * 递增任务调度
 *
 * @ClassName: IncreasingIntervalScheduler
 * @Author: TXC
 * @Date: 2024-12-13 15:32
 **/
public class IncreasingIntervalScheduler {
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    /**
     * 初始间隔
     */
    private final AtomicInteger interval;
    /**
     * 执行次数
     */
    private final AtomicInteger executionCount = new AtomicInteger(0);
    /**
     * 最大执行次数
     */
    private final int maxExecutions;
    /**
     * 递增间隔
     */
    private final int increasingInterval;
    private final Supplier<Boolean> taskCallSupplier; // 接口调用的Supplier

    private IncreasingIntervalScheduler(int maxExecutions, int initialInterval, int increasingInterval,
                                        Supplier<Boolean> taskCallSupplier) {
        interval = new AtomicInteger(initialInterval);
        this.maxExecutions = maxExecutions;
        this.increasingInterval = increasingInterval;
        this.taskCallSupplier = taskCallSupplier;
    }

    public void start() {
        scheduleNextTask();
    }

    private void scheduleNextTask() {
        if (executionCount.get() > maxExecutions) {
            shutdown();
            return;
        }
        Runnable task = () -> {
            try {
                boolean isSuccess = taskCallSupplier.get();
                if (!isSuccess) {
                    // 增加间隔时间
                    interval.addAndGet(increasingInterval);
                    // 重新安排下一次任务
                    scheduleNextTask();
                } else {
                    shutdown();
                }
            } catch (Exception e) {
                // 增加间隔时间
                interval.addAndGet(increasingInterval);
                // 重新安排下一次任务
                scheduleNextTask();
            }
        };
        // 安排任务在指定间隔后执行
        scheduler.schedule(task, interval.get(), TimeUnit.SECONDS);
    }

    public void shutdown() {
        scheduler.shutdown();
    }

    public static class Builder {
        private int maxExecutions;
        private int initialInterval;
        private int increasingInterval;
        private Supplier<Boolean> taskCallSupplier;


        public Builder setMaxExecutions(int maxExecutions) {
            this.maxExecutions = maxExecutions;
            return this;
        }

        public Builder setInitialInterval(int initialInterval) {
            this.initialInterval = initialInterval;
            return this;
        }

        public Builder setIncreasingInterval(int increasingInterval) {
            this.increasingInterval = increasingInterval;
            return this;
        }

        public Builder setCallSupplier(Supplier<Boolean> taskCallSupplier) {
            this.taskCallSupplier = taskCallSupplier;
            return this;
        }

        public IncreasingIntervalScheduler build() {
            return new IncreasingIntervalScheduler(maxExecutions, initialInterval, increasingInterval,
                    taskCallSupplier);
        }
    }
}
